﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Intelligent.OTC.Domain.DataModel;
using Intelligent.OTC.Common;
using Intelligent.OTC.Business;
using System.Configuration;
using System.IO;
using Intelligent.OTC.Common.Utils;
using Intelligent.OTC.WebApi.Core;
using Intelligent.OTC.Domain;
using Intelligent.OTC.Common.Exceptions;
using System.Web.OData;
using Intelligent.OTC.Common.Attr;


namespace Intelligent.OTC.WebApi.Controllers
{
    [UserAuthorizeFilter(actionSet: "master")]
    public class CustomerPaymentcircleController : ApiController
    {

        [HttpGet]
        [PagingQueryable]
        public IEnumerable<CustomerPaymentCircle> Get()
        {
            CustomerPaymentCircleService service = SpringFactory.GetObjectImpl<CustomerPaymentCircleService>("CustomerPaymentCircleService");
            var cusPaymentcircleList = service.GetCustomerPaymentCircle();
            return cusPaymentcircleList.AsQueryable<CustomerPaymentCircle>();
        }

       [HttpGet]
        public IEnumerable<CustomerPaymentCircle> Get(string num)
        {
            CustomerPaymentCircleService service = SpringFactory.GetObjectImpl<CustomerPaymentCircleService>("CustomerPaymentCircleService");
            var cusPaymentcircleList = service.GetCustPaymentCircle(num);
            List<CustomerPaymentCircleP> CustomerPaymentcircleList = new List<CustomerPaymentCircleP>();
            CustomerPaymentCircleP cuspaycircle = new CustomerPaymentCircleP();
            int ind = 0;
            foreach (var item in cusPaymentcircleList) {
                ind++;
                cuspaycircle = new CustomerPaymentCircleP();
                cuspaycircle.Id = item.Id;
                cuspaycircle.PaymentDay = item.PaymentDay;
             //   cuspaycircle.PaymentWeek = item.PaymentWeek;
                cuspaycircle.weekDay = (item.PaymentDay.DayOfWeek).ToString();
                cuspaycircle.Flg = item.Flg;
                cuspaycircle.Description = item.Description;
                cuspaycircle.CustomerNum = item.CustomerNum;
                cuspaycircle.CreatePersonId = item.CreatePersonId;
                cuspaycircle.CreateDate = item.CreateDate;
                cuspaycircle.LegalEntity = item.LegalEntity;
                cuspaycircle.sortId = ind;
                CustomerPaymentcircleList.Add(cuspaycircle);
            }
            return CustomerPaymentcircleList.AsQueryable<CustomerPaymentCircle>();
        }


        [HttpPost]
        public string Post([FromBody] List<string> pay)
        {
            CustomerPaymentCircleService service = SpringFactory.GetObjectImpl<CustomerPaymentCircleService>("CustomerPaymentCircleService");
            return service.AddPamentCircle(pay);
            //    return "Add PaymentCalendar Successful!";
        }


        [HttpPost]
        public string UploadCircle(string customerNum, string legal)
        {
                CustomerPaymentCircleService service = SpringFactory.GetObjectImpl<CustomerPaymentCircleService>("CustomerPaymentCircleService");
                return service.AddUploadCircle(customerNum,legal);
              //  return "Uploaded file Successful!";
        }


        [HttpPost]
        public void delete(int id)
        {
            CustomerPaymentCircleService service = SpringFactory.GetObjectImpl<CustomerPaymentCircleService>("CustomerPaymentCircleService");
            CustomerPaymentCircle old = service.CommonRep.FindBy<CustomerPaymentCircle>(id);
            if (old != null)
            {
                service.CommonRep.Remove(old);
                service.CommonRep.Commit();
            }
        }

        [HttpGet]
        [PagingQueryable]
        public IQueryable<CustomerPaymentCircle> GetLegalEntity(string custNum, string legal)
        {
            CustomerPaymentCircleService service = SpringFactory.GetObjectImpl<CustomerPaymentCircleService>("CustomerPaymentCircleService");
            var cusPaymentcircleList = service.GetCircleByCondtion(custNum,legal);
            return cusPaymentcircleList.AsQueryable<CustomerPaymentCircle>();
        }
    }
}